package com.zjy.financialsystemjavafx.controller;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.event.ActionEvent;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.stage.Modality;
import java.io.IOException;
import com.zjy.financialsystemjavafx.service.UserService;
import com.zjy.financialsystemjavafx.model.entity.User;
import com.zjy.financialsystemjavafx.util.SessionContext;

/**
 * 登录界面控制器
 */
public class LoginController {

    @FXML
    private TextField usernameField;

    @FXML
    private PasswordField passwordField;

    @FXML
    private Button loginButton;

    @FXML
    private Button registerButton;

    /**
     * 处理登录操作
     */
    @FXML
    private void handleLogin(ActionEvent event) {
        String username = usernameField.getText();
        String password = passwordField.getText();
        try {
            User user = new UserService().authenticate(username, password);
            if (user != null) {
                // 保存会话
                SessionContext.setCurrentUser(user);
                // 跳转仪表盘
                Parent root = FXMLLoader.load(getClass().getResource("/com/zjy/financialsystemjavafx/view/Dashboard.fxml"));
                Scene scene = new Scene(root);
                scene.getStylesheets().add(getClass().getResource("/css/app.css").toExternalForm());
                Stage stage = (Stage) loginButton.getScene().getWindow();
                stage.setTitle("个人财务管家 - 仪表盘");
                stage.setScene(scene);
                stage.show();
            } else {
                Alert alert = new Alert(AlertType.WARNING);
                alert.setTitle("登录失败");
                alert.setHeaderText(null);
                alert.setContentText("用户名或密码错误");
                alert.showAndWait();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Alert alert = new Alert(AlertType.ERROR);
            alert.setTitle("登录错误");
            alert.setHeaderText(null);
            alert.setContentText("登录过程中出现异常: " + e.getMessage());
            alert.showAndWait();
        }
    }

    /**
     * 处理注册操作
     */
    @FXML
    private void handleRegister(ActionEvent event) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("/com/zjy/financialsystemjavafx/view/Register.fxml"));
            Stage stage = new Stage();
            stage.setTitle("个人财务管家 - 注册");
            stage.setScene(new Scene(root));
            stage.initOwner(loginButton.getScene().getWindow());
            stage.initModality(Modality.APPLICATION_MODAL);
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
            Alert alert = new Alert(AlertType.ERROR);
            alert.setTitle("加载错误");
            alert.setHeaderText(null);
            alert.setContentText("无法打开注册界面");
            alert.showAndWait();
        }
    }
} 